package view
{
	import data.Globals;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextFormat;
	
	import interfaces.IGameView;
	
	import manager.ObjDataManage;
	
	import view.Arm.BaseTank;
	import view.monster.BaseMonster;
	import view.other.BulletHoleView;
	import view.plant.PlantItem;
	import view.tip.MonsterTip;
	import view.tip.PlantTip;
	import view.tip.TankTip;

	public class GameView extends Sprite implements IGameView
	{
		private var gradMap:GradMap;
		
		private var backgroundView:BackgroundView;
		
		private var monsterView:MonsterView;
		
		private var plantView:PlantView;
		
		private var tankView:TankView;
		
		private var bulletHole:BulletHoleView;
		
		private var bulletView:BulletView;
		
		private var goodMenuView:GoodMenuView;
		
		private var explodeView:ExplodeView;
		
		private var energyPlusView:EnergyPlusView;
		
		private var animationView:AnimationView;
		
		private var mouseTipView:MouseTipView;
		
		private var captionView:CaptionView;
		
		private var plantTip:PlantTip;
		
		private var monsterTip:MonsterTip;
		
		private var tankTip:TankTip;
		
		private var alert:AlertView;
		
		private var tips:TipsView;
		
		public function GameView()
		{
			initFace();	
			Globals.gameView = this;
		}
		
		private function initFace():void
		{
			backgroundView = new BackgroundView();
			this.addChild(backgroundView);
			
			gradMap = new GradMap();
			this.addChild(gradMap);
			
			monsterView = new MonsterView();
			this.addChild(monsterView);
			
			plantView = new PlantView();
			this.addChild(plantView);
			
			tankView = new TankView();
			this.addChild(tankView);
			
			bulletHole = new BulletHoleView();
			this.addChild(bulletHole);
			
			bulletView = new BulletView();
			this.addChild(bulletView);
			
			goodMenuView = new GoodMenuView();
			this.addChild(goodMenuView);
			
			explodeView = new ExplodeView();
			this.addChild(explodeView);
			
			energyPlusView = new EnergyPlusView();
			this.addChild(energyPlusView);
			
			animationView = new AnimationView();
			this.addChild(animationView);
			
			mouseTipView = new MouseTipView();
			
			captionView = new CaptionView();
			
			plantTip = new PlantTip();
			
			monsterTip = new MonsterTip();
			
			tankTip = new TankTip();
			
			tips = new TipsView();

			alert = new AlertView();
		}		
		
		public function playAnimation(type:String,back:Function=null):void{
			if(type == AnimationView.COUNT_DOWN){
				resetView();
				backgroundView.initface();
				goodMenuView.initface();
				gradMap.drawLine();
				plantView.initface();
				goodMenuView.show();
				goodMenuView.isPause = false;
				tankView.initface();
			}
			animationView.playClip(type,back);
		}
		
		public function start():void
		{
			monsterView.initMonster();
			showTips(ObjDataManage.getInstance().tips[int(Math.random()*ObjDataManage.getInstance().tips.length)]);
			trace('game_start');
		}
		
		public function produceTank(type:String,p:Point):void{
			tankView.productionTank(type,p);
		}
		
		public function getMonsterAry():Array{
			return monsterView.aMonster;
		}
		
		public function getTankAry():Array{
			return tankView.tankAry;
		}
		
		/**
		 *发射子弹 type 子弹类型 p:Point 子弹坐标
		 */
		public function shoutBullet(type:String,p:Point,tar_p:Point,tar:BaseTank = null,posX:int=0,posY:int=0,shoutTarget:DisplayObject=null):void{
			bulletView.createBullet(type,p,tar_p,tar,posX,posY,shoutTarget);
		}
		
		public function resetView():void{
			backgroundView.clearView();
			monsterView.clearView();
			plantView.clearView();
			tankView.clearView();
			bulletView.clearView();
			goodMenuView.clearView();
			animationView.clearView();
			energyPlusView.clearView();
		}
		
		public function getCurArmType():String{
			return goodMenuView.curTankType;
		}
		public function showAlert(txt:String,alertCallback:Function,yes:Boolean,no:Boolean,format:TextFormat = null):void{
			alert.setTxt(txt,format);
			alert.show(alertCallback,yes,no);
			this.addChild(alert);
		}
		public function showCaption(str:String,callback:Function,tfmat:TextFormat=null):void{
			captionView.show(str,callback,tfmat);
			this.addChild(captionView);
		}
		public function showMouseTip(type:String):void{
			mouseTipView.show(type);
			this.addChild(mouseTipView);
		}
		public function calculateScore(value:int):void{
			goodMenuView.calculateScore(value);
		}
		public function createExplode(_name:String,_x:int=0,_y:int=0,_rotate:int = 0,scale:Number = 0):void{
			explodeView.createExplode(_name,_x,_y,_rotate,scale);
		}
		public function setGradIsCanSite(_x:int,_y:int,boo:Boolean):void{
			gradMap.setGradIsCanSite(_x,_y,boo);
		}
		public function initPlant():void{
			plantView.initface();
		}
		public function get targetPlant():PlantItem{
			return plantView.targetPlant;
		}
		public function set targetPlant(value:*):void{
			plantView.targetPlant = value;
		}
		public function get targetMonster():BaseMonster{
			return monsterView.selecet;
		}
		public function set targetMonster(value:*):void{
			monsterView.selecet = value;
		}
		public function getPlantAry():Array{
			return plantView.ary;
		}
		public function showMouseEffect(_x:int,_y:int):void{
			mouseTipView.showMouseEffect(_x,_y);
		}
		public function createEnergyMotion(energy:int,_x:int,_y:int):void{
			energyPlusView.create(energy,_x,_y);
		}
		public function setWave(cur:int,total:int):void{
			goodMenuView.setWave(cur,total);
		}
		public function showPlantTip(isShow:Boolean,enery:int = 0,hp:int = 0,_x:int=0,_y:int=0):void{
			isShow ? plantTip.show(enery,hp,_x,_y) : plantTip.hide();
		}
		public function showTips(str:String):void{
			tips.show(str);
		}
		public function showMonsterTip(isShow:Boolean,_x:int  =0,_y:int = 0,hp:int = 0,energy:int = 0,exp_rad:int = 0,hurt:int = 0):void{
			isShow ? monsterTip.show(_x,_y,hp,energy,exp_rad,hurt) : monsterTip.hide();
		}
		public function showTankTip(isShow:Boolean,_x:int=0,_y:int=0,hp:int=0,shoutRadius:int=0,rotate:int=0,shoutRate:int=0):void{
			isShow ? tankTip.show(_x,_y,hp,shoutRadius,rotate,shoutRate) : tankTip.hide();
		}
		public function createBulletHole(p1:Point,p2:Point):void{
			bulletHole.createBulletHole(p1,p2);
		}
	}
}